
const fs = require("fs")


const LV = 50

const Temp_IV = {
    "hp": 31,
    "at": 31,
    "de": 31,
    "sa": 31,
    "sd": 31,
    "sp": 31,
}

const Temp_EV = {
    "hp": 63,
    "at": 63,
    "de": 63,
    "sa": 63,
    "sd": 63,
    "sp": 63,
}

const Temp_Xinge = {
    "hp": 1,
    "at": 1.1,
    "de": 1.1,
    "sa": 1.1,
    "sd": 1.1,
    "sp": 1.1,
}




function calcShuXin(race, other={},IV=Temp_IV, EV=Temp_EV, xinge=Temp_Xinge, lv=LV){
    let ret = {}
    for (baseKey in race){
        ret[baseKey] = (2 * parseInt(race[baseKey]) + IV[baseKey] + (EV[baseKey]||0)) * lv / 100 + 5
        if (xinge[baseKey]){
            ret[baseKey] = ret[baseKey] * xinge[baseKey]
        }
        if (other[baseKey]){
            ret[baseKey] = ret[baseKey] * other[baseKey]
        }
    }
    ret["hp"] = ret["hp"] + 5 + lv
    for (k in ret){
        ret[k] = parseInt(ret[k])
    }
    return ret
}


function calcDmg(at, df,skillDmg,otherdmgChange = 1.5,lv=LV) {
    let ret = parseInt((2*lv + 10)/250 * skillDmg * ( at/df ) +2) * (otherdmgChange)
    return [parseInt(ret*0.85), parseInt(ret)]
}

let race1 = {  // template 
    name: "quan100",
    race: {
        "hp": 100,
        "at": 100,
        "de": 100,
        "sa": 100,
        "sd": 100,
        "sp": 100,
    }
    
}




let mukeyin = {
    name: "mukeyin",
    race: {
        "hp": 85,
        "at": 120,
        "de": 70,
        "sa": 50,
        "sd": 60,
        "sp": 100,
    },
    EV: {
        "at": 63,
        "sp": 63,
    },
    xingge: {
        "at": 1.1,
        "sa": 0.9,
    },
    data_other: {
        "at": 1.5,
    },
    skillDmg: 120,
    otherdmgChange: 1.5*1.2
}

let daziopi = {
    name: "dazuiou",
    race: {
        "hp": 60,
        "at": 50,
        "de": 100,
        "sa": 95,
        "sd": 70,
        "sp": 65,
    },
    EV: {
        "sa": 63,
        "hp": 63,
    },
    xingge: {
        "sa": 1.1,
        "at": 0.9,
    },
    data_other: {
        "sa": 1.5,
    },
    skillDmg: 110,
    otherdmgChange: 1.5*1.5
}

let meitangui = {
    name: "meitangui",
    race: {
        "hp": 70,
        "at": 85,
        "de": 140,
        "sa": 85,
        "sd": 70,
        "sp": 20,
    },
    EV: {
        "sa": 63,
        "hp": 63,
    },
    xingge: {
        "sa": 1.1,
        "at": 0.9,
    },
    data_other: {
        "sa": 1.5,
    },
    skillDmg: 150,
    otherdmgChange: 1.5*1.5
}

let zhanchuilong = {
    name: "zhanchuilong",
    race: {
        "hp": 97,
        "at": 165,
        "de": 60,
        "sa": 65,
        "sd": 50,
        "sp": 58,
    },
    EV: {
        "at": 63,
        "de": 63,
    },
    xingge: {
        "at": 1.1,
        "sa": 0.9,
    },
    data_other: {
        "at": 1.5,
    },
    skillDmg: 150,
    otherdmgChange: 1.5
}

let megajujin = {
    name: "megajujin",
    race: {
        "hp": 80,
        "at": 145,
        "de": 150,
        "sa": 105,
        "sd": 110,
        "sp": 110,
    },
    EV: {
        "at": 63,
        "sp": 63,
    },
    xingge: {
        "at": 1.1,
        "sa": 0.9,
    },
    skillDmg: 90,
    otherdmgChange: 1.5 * 1.3
}

let megabaofeilong = {
    name: "megabaofeilong",
    race: {
        "hp": 95,
        "at": 145,
        "de": 130,
        "sa": 120,
        "sd": 90,
        "sp": 120,
    },
    EV: {
        "at": 63,
        "hp": 63,
    },
    xingge: {
        "at": 1.1,
        "sa": 0.9,
    },
    skillDmg: 120,
    otherdmgChange: 1.5 * 1.2
}

let megahudi = {
    name: "megahudi",
    race: {
        "hp": 55,
        "at": 50,
        "de": 65,
        "sa": 175,
        "sd": 105,
        "sp": 150,
    },
    EV: {
        "sa": 63,
        "sp": 63,
    },
    xingge: {
        "sa": 1.1,
        "at": 0.9,
    },
    skillDmg: 90,
    otherdmgChange: 1.5 
}

let baozhatouniu = {
    name: "baozhatouniu",
    race: {
        "hp": 95,
        "at": 110,
        "de": 95,
        "sa": 40,
        "sd": 95,
        "sp": 55,
    },
    EV: {
        "at": 63,
        "de": 63,
    },
    xingge: {
        "at": 1.1,
        "sa": 0.9,
    },
    skillDmg: 120,
    data_other: {
        "at": 1.5,
    },
    otherdmgChange: 1.5 * 1.2
}

let cijiabei = {
    name: "cijiabei",
    race: {
        "hp": 50,
        "at": 95,
        "de": 180,
        "sa": 85,
        "sd": 45,
        "sp": 70,
    },
    EV: {
        "at": 63,
        "sp": 63,
    },
    xingge: {
        "at": 1.1,
        "sa": 0.9,
    },
    skillDmg: 25,
    data_other: {
        "at": 2,
        "de": 0.5,
        "sp": 2
    },
    otherdmgChange: 1.5 * 5
}

let daidaishou = {
    name: "daidaishou",
    race: {
        "hp": 95,
        "at": 75,
        "de": 110,
        "sa": 100,
        "sd": 80,
        "sp": 30,
    },
    EV: {
        "sa": 63,
        "de": 63,
    },
    xingge: {
        "sa": 1.1,
        "at": 0.9,
    },
    skillDmg: 90,
    otherdmgChange: 1.5 * 1.3
}

let juqianxie = {
    name: "juqianxie",
    race: {
        "hp": 55,
        "at": 130,
        "de": 115,
        "sa": 50,
        "sd": 50,
        "sp": 75,
    },
    EV: {
        "at": 63,
        "hp": 63,
    },
    xingge: {
        "at": 1.1,
        "sa": 0.9,
    },
    data_other: {
        "at": 1.5,
    },
    skillDmg: 100,
    otherdmgChange: 1.5 * 1.5
}

let dawangyan = {
    name: "dawangyan",
    race: {
        "hp": 60,
        "at": 85,
        "de": 60,
        "sa": 75,
        "sd": 50,
        "sp": 125,
    },
    EV: {
        "at": 63,
        "hp": 63,
    },
    xingge: {
        "at": 1.1,
        "sa": 0.9,
    },
    data_other: {
        "at": 1.5 * 1.5,
    },
    skillDmg: 120,
    otherdmgChange: 1.5 
}


let guizujukai = {
    name: "guizujukai",
    race: {
        "hp": 72,
        "at": 105,
        "de": 115,
        "sa": 54,
        "sd": 86,
        "sp": 68,
    },
    EV: {
        "at": 63,
        "de": 63,
    },
    xingge: {
        "at": 1.1,
        "sa": 0.9,
    },
    data_other: {
        "at": 2,
        "sp": 2,
        "de": 0.5,
        "sd": 0.5,
    },
    skillDmg: 85,
    otherdmgChange: 1.5 * 1.3 * 1.3
}

let bkms = [
    mukeyin,daziopi,meitangui,zhanchuilong,megajujin,megabaofeilong,megahudi,baozhatouniu,cijiabei,daidaishou,juqianxie,
    dawangyan, guizujukai
]


let bkmCalcs = []

bkms.forEach(p => {
    bkmCalcs.push({
        name: p.name,
        calc: calcMoni(p , race1)
    })
})
 

bkmCalcs = bkmCalcs.sort((b, a) => a.calc.p1_atk_rate - b.calc.p1_atk_rate)
fs.writeFileSync("./tmp_atk.js", JSON.stringify(bkmCalcs))
bkmCalcs = bkmCalcs.sort((a, b) => a.calc.p2_atk_rate - b.calc.p2_atk_rate)
fs.writeFileSync("./tmp_def.js", JSON.stringify(bkmCalcs))
bkmCalcs = bkmCalcs.sort((b, a) => (a.calc.p1_atk_rate-0.5+(0.5-a.calc.p2_atk_rate)) - (b.calc.p1_atk_rate-0.5+(0.5-b.calc.p2_atk_rate)))
fs.writeFileSync("./tmp_xhgg.js", JSON.stringify(bkmCalcs))

function calcMoni(p1, p2) {
    let p1_data = calcShuXin(p1.race, (p1.data_other||{}), Temp_IV, (p1.EV || Temp_EV),(p1.xingge||Temp_Xinge),LV )
    let p2_data = calcShuXin(p2.race, (p2.data_other||{}), Temp_IV, (p2.EV || Temp_EV),(p2.xingge||Temp_Xinge),LV )
    
    let p1_atk_shux = "at"
    let p1_atk_shux2 = "de"
    if (p1_data.at < p1_data.sa) { p1_atk_shux = "sa";p1_atk_shux2="sd" }
    let p1_atk_p2 = calcDmg(p1_data[p1_atk_shux], p2_data[p1_atk_shux2],(p1.skillDmg||90),(p1.otherdmgChange||1.5),LV)
    let p2_atk_shux = "at"
    let p2_atk_shux2 = "de"
    if (p1_data.de < p1_data.sd) { p2_atk_shux = "sa";p2_atk_shux2="sd" }
    let p2_atk_p1 = calcDmg(p2_data[p2_atk_shux], p1_data[p2_atk_shux2],(p2.skillDmg||90),(p2.otherdmgChange||1.5),LV)


    let ret = {
        p1_data,
        p2_data,
        p1_atk_p2,
        p1_atk_rate: (p1_atk_p2[1]/p2_data.hp).toFixed(3)-0,
        p2_atk_p1,
        p2_atk_rate: (p2_atk_p1[1]/p1_data.hp).toFixed(3)-0
    }
    return ret
}




